﻿namespace Sharp.Portal.Mvc.Bootstrap
{
    /// <summary>
    /// 按钮扩展方法。
    /// </summary>
    public static class LinkButtonExtensions
    {
        public static LinkButtonBuilder LinkButton(this BootstrapFactory factory)
        {
            return new LinkButtonBuilder();
        }

        public static LinkButtonBuilder LinkButton(this BootstrapFactory factory, string title, ToolBarIco ico)
        {
            string icoString;
            switch (ico)
            {
                case ToolBarIco.Details:
                    icoString = "fa fa-files-o";
                    break;
                case ToolBarIco.Create:
                    icoString = "fa fa-plus";
                    break;
                case ToolBarIco.Edit:
                    icoString = "fa fa-pencil";
                    break;
                case ToolBarIco.Delete:
                    icoString = "fa fa-trash";

                    break;
                case ToolBarIco.Import:
                    icoString = "fa fa-file-excel-o";
                    break;
                case ToolBarIco.Export:
                    icoString = "fa fa-file-text-o";
                    break;
                case ToolBarIco.Permission:
                    icoString = "fa fa-paragraph";
                    break;
                case ToolBarIco.Refresh:
                    icoString = "fa fa-refresh";
                    break;
                case ToolBarIco.Save:
                    icoString = "fa fa-save";
                    break;
                case ToolBarIco.Close:
                    icoString = "fa fa-close";
                    break;
                case ToolBarIco.Back:
                    icoString = "fa fa-backward";
                    break;
                default:
                    icoString = string.Empty;
                    break;
            }

            var linkbutton = LinkButton(factory, title, icoString);

            switch (ico)
            {
                case ToolBarIco.Create:
                    linkbutton.Style().Primary();
                    break;
                case ToolBarIco.Delete:
                    linkbutton.Style().Danger();
                    break;
            }

            return linkbutton;
        }
        public static LinkButtonBuilder LinkButton(this BootstrapFactory factory, string title, string ico)
        {
            var bb = factory.LinkButton().Title(title);
            bb.TagBuilder.InnerHtml = $"<span>{ bb.TagBuilder.InnerHtml }</span>";

            bb.TagBuilder.InnerHtml = string.Format("<i class=\"{0}\"></i> {1}", ico, bb.TagBuilder.InnerHtml);

            return bb;
        }


        public static LinkButtonBuilder LoadingText(this LinkButtonBuilder factory, string text)
        {
            return factory.Attribute("data-loading-text", text, true);
        }

        /// <summary>
        /// 按钮样式
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static LinkButtonBuilder Style(this LinkButtonBuilder factory, BootstrapStyle type)
        {
            if (type.HasFlag(BootstrapStyle.Default))
            {
                factory.Style().Default();
            }
            if (type.HasFlag(BootstrapStyle.Primary))
            {
                factory.Style().Primary();
            }
            if (type.HasFlag(BootstrapStyle.Success))
            {
                factory.Style().Success();
            }
            if (type.HasFlag(BootstrapStyle.Info))
            {
                factory.Style().Info();
            }
            if (type.HasFlag(BootstrapStyle.Warning))
            {
                factory.Style().Warning();
            }
            if (type.HasFlag(BootstrapStyle.Danger))
            {
                factory.Style().Danger();
            }
            if (type.HasFlag(BootstrapStyle.Block))
            {
                factory.Style().Block();
            }
            if (type.HasFlag(BootstrapStyle.Flat))
            {
                factory.Style().Flat();
            }


            return factory;
        }

        /// <summary>
        /// 访问路径，用来作权限判断的
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="route">路由</param>
        /// <returns></returns>
        public static LinkButtonBuilder Route(this LinkButtonBuilder factory, string route)
        {
            return factory.Attribute("data-route", route, true);
        }
        /// <summary>
        /// 权限代码用来作权限判断
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="code">权限代码</param>
        /// <returns></returns>
        public static LinkButtonBuilder Permission(this LinkButtonBuilder factory, string code)
        {
            return factory.Attribute("data-permission", code, true);
        }
    }
    /// <summary>
    /// 工具栏按钮图标
    /// </summary>
    public enum ToolBarIco
    {
        /// <summary>
        /// 不设置图标
        /// </summary>
        None,
        /// <summary>
        /// 新增
        /// </summary>
        Create,
        /// <summary>
        /// 修改
        /// </summary>
        Edit,
        /// <summary>
        /// 删除
        /// </summary>
        Delete,
        /// <summary>
        /// 导入
        /// </summary>
        Import,
        /// <summary>
        /// 导出
        /// </summary>
        Export,
        /// <summary>
        /// 权限
        /// </summary>
        Permission,
        /// <summary>
        /// 刷新
        /// </summary>
        Refresh,
        /// <summary>
        /// 审核
        /// </summary>
        Checkin,
        /// <summary>
        /// 详情
        /// </summary>
        Details,
        /// <summary>
        /// 保存
        /// </summary>
        Save,
        /// <summary>
        /// 返回
        /// </summary>
        Back,
        /// <summary>
        /// 关闭
        /// </summary>
        Close
    }

}